#include <stdio.h>

int kf(int b, int p)
{
	int i, s;
	s = 1;
	for (i=0; i<p; i++) {
		s *= b;
	}

	return s;
}

int main(int argc, char *argv[])
{
	int k, n, s[33], w, i, t;
	scanf("%d %d", &k, &n);

	w = 0;
	while (n != 0) {
		s[w] = n % 2;
		n /= 2;
		w++;
	}

	/*
	printf("%d\n", w);
	for (i=0; i<w; i++) {
		printf("%d", s[i]);
	}
	*/

	t = 0;
	for (i=0; i<w; i++) {
		t = t + s[i] * kf(k, i);
	}
	printf("%d\n", t);

	return 0;
}
